package com.easycarebn.main.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.easycarebn.main.utils.IResult;
import com.easycarebn.main.utils.Utils;
import com.easycarebn.request.data.DoctorDetail;
import com.easycarebn.request.data.NotificationItem;
import com.easycarebn.request.data.UserResponse;
import com.squareup.picasso.Picasso;
import com.timbacsi.main.R;

public class MessageAdapter extends BaseAdapter {
	ViewHolder holder;
	Context mContext;
	List<NotificationItem> arr;
	private IResult callIResult;

	public MessageAdapter(Context mContext, List<NotificationItem> arr,
			IResult callIResult) {
		this.mContext = mContext;
		this.arr = arr;
		this.callIResult = callIResult;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arr.size();
	}

	public void removeItem(int position) {
		for (int i = arr.size() - 1; i >= 0; i--) {
			if (i == position) {
				arr.remove(position);
			}
		}
		notifyDataSetChanged();
	}

	@Override
	public NotificationItem getItem(int position) {
		// TODO Auto-generated method stub
		return arr.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public class ViewHolder {
		public TextView title;
		public TextView time;
		public TextView content;

		public ImageView icon;

		public Button btnDaxem;
		public Button btnChitiet;
		public Button btnXoa;
		public Button btnTraloi;

	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			// Utilities.log("convert null " + position);
			holder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.item_message, null);
			holder.title = (TextView) convertView.findViewById(R.id.name);
			holder.time = (TextView) convertView.findViewById(R.id.speciality);
			holder.content = (TextView) convertView.findViewById(R.id.placeofwork);

			holder.icon = (ImageView) convertView
					.findViewById(R.id.icon);
			holder.btnChitiet = (Button) convertView
					.findViewById(R.id.btnChitiet);
			holder.btnXoa = (Button) convertView.findViewById(R.id.btnXoa);
			holder.btnDaxem = (Button) convertView.findViewById(R.id.btnDaxem);
			holder.btnTraloi = (Button) convertView
					.findViewById(R.id.btnTraloi);

			convertView.setTag(holder);
		} else {
			// Utilities.log("convert not null " + position);
			holder = (ViewHolder) convertView.getTag();
		}
		final NotificationItem item = getItem(position);
	    Picasso.with(mContext).load(item.getSent_by_avatar_thumbs()).placeholder(R.drawable.avata).error(R.drawable.avata).into(holder.icon);

		if (item.getSent_by_name() == null
				|| item.getSent_by_name().length() == 0) {
			Utils.setTextEmpty(holder.title, (Activity) mContext);
		} else {
			holder.title.setText(item.getSent_by_name());
		}
		if (item.getStatus() == 1) {
			holder.btnDaxem.setVisibility(View.VISIBLE);
		} else {
			holder.btnDaxem.setVisibility(View.GONE);
		}
		
		if (item.getReply_for_notification_id()!=-1) {
			holder.btnTraloi.setVisibility(View.VISIBLE);
		} else {
			holder.btnTraloi.setVisibility(View.GONE);
		}

		holder.content.setText(Html.fromHtml(item.getContent()));
		holder.time.setText(String.format(mContext.getResources().getString(R.string.gui_ngay), item.getCreatedAt()));

		holder.btnXoa.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				callIResult.onDisplay(Utils.REMOVE_SMS, position + "");
			}
		});
		holder.btnChitiet.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				callIResult.onDisplay(Utils.DOCTOR_SERVICE_DETAILT, position + "");
			}
		});

		holder.btnTraloi.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				UserResponse userResponse = Utils.getUserLogin(mContext);
				if (userResponse != null) {
					DoctorDetail docter = new DoctorDetail();
					docter.setName(item.getSent_by_name());
					docter.setDoctor_id(item.getSent_by());
					Utils.sendMessagesToDocter((Activity) mContext,
							userResponse.getToken(), docter, item.getReply_for_notification_id());
				}
			}
		});

		return convertView;
	}
}
